#include "mem_allocator.h"
#include <cstring>
#include <iostream>

int32_t main() {
  int32_t main_res{0};

  try {
    const std::shared_ptr<MemTest::MemAllocDelegate> mem_delegate = MemTest::MemAllocDelegate::get_instance();

    mem_delegate->setup_live_direct_memory_mode(0, 0);

    int8_t *const buffer1 = mem_delegate->mem_calloc(100, 4, false);
    if (buffer1 != nullptr) {
      std::cout << "Memory allocated by: " << mem_delegate->get_allocator_name(buffer1) << std::endl;

      const char *const str1 = "Hello, World!";
      strncpy(reinterpret_cast<char *>(buffer1), str1, 100);

      std::cout << "Content of buffer1: " << reinterpret_cast<char *>(buffer1) << std::endl;
    } else {
      std::cout << "Memory allocation failed." << std::endl;
    }

    int8_t *const buffer2 = mem_delegate->mem_calloc(50, 8, true);
    if (buffer2 != nullptr) {
      std::cout << "Memory allocated by: " << mem_delegate->get_allocator_name(buffer2) << std::endl;

      const char *const str2 = "This is a test.";
      strncpy(reinterpret_cast<char *>(buffer2), str2, 50);

      std::cout << "Content of buffer2: " << reinterpret_cast<char *>(buffer2) << std::endl;
    } else {
      std::cout << "Memory allocation failed." << std::endl;
    }

    mem_delegate->mem_free(buffer1);
    mem_delegate->mem_free(buffer2);
  } catch (...) {
    std::cerr << "Exception occurred" << std::endl;
    main_res = 1;
  }

  return main_res;
}
